//
//  QDTabBarController.m
//  QDMetroClient
//
//  Created by YueWen on 2018/12/25.
//  Copyright © 2018 YueWen. All rights reserved.
//

#import "QDTabBarController.h"
#import "COHBDNavigationBar.h"
#import "HBDNavigationController.h"
#import "QDMainTabBar.h"
#import "QDMetroClient-Swift.h"
#import <objc/runtime.h>


@implementation UITabBarController (JCBrightness)

- (UIViewController *)currentViewController {
    return self.childViewControllers[self.selectedIndex];
}

@end




@interface QDTabBarController ()<UITabBarControllerDelegate>

/// 自定义tabBar
@property (nonatomic, strong) QDMainTabBar *tabBarView;

@end

@implementation QDTabBarController


+ (instancetype)sharedInstance
{
    static id shareInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstance = [self new];
    });
    
    return shareInstance;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 设置tabBar
    [self setValue:self.tabBarView forKey:@"tabBar"];
    // 追加控制器
    [self addChildViewControllers];
    // default is subway's code
//    self.selectedIndex = 0;
    
    self.delegate = self;
    [self.tabBarView.largeButton addTarget:self action:@selector(largeButtonDidTap) forControlEvents:UIControlEventTouchUpInside];
    
    // 追加相应3D touch的通知
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(didReceiverShortcutItemNotification:) name:@"QDShortcutItemCompleteNotificationName" object:nil];
}



- (void)didReceiverShortcutItemNotification:(NSNotification *)notification {
    
    //获得type,默认type为qd_metro
    NSString *type = [notification.userInfo valueForKey:@"type"] ?: @"qd_metro";
    
    //修改本地类型
    [QDCodeSettingConfiguration changedCodeSettingTypeForShortItemToOCWithType:type];
    
    //pop所有的控制器
    UINavigationController *selectViewController = self.childViewControllers[self.selectedIndex];
    
    [selectViewController popToRootViewControllerAnimated:false];
    
    //如果不是扫码，进入扫码
    [self largeButtonDidTap];
    
    //发送主动切换通知
    [NSNotificationCenter.defaultCenter postNotificationName:@"QDShortcutItemDidTapFromtTabNotificationName" object:nil];
}



- (void)largeButtonDidTap
{
    //进行判断
    if (self.selectedIndex == 1) {//表示已经点击了扫码
        
        return;
    }
    self.selectedIndex = 1;
}




- (UIViewController *)visibleViewController {
    return ((UINavigationController *)self.currentViewController).visibleViewController;
}


- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}


/// 追加主页的三个tabChildController
- (void)addChildViewControllers {
    
    // tabItem's titles
    NSArray <NSString *> *titles = @[@"首页",@"乘车码",@"我的"];
    NSArray <NSString *> *images = @[@"tab_home",@"",@"tab_mine"];
    
    // add tabController's childViewController
    for (NSInteger i = 0; i < titles.count; i++) {
        [self addChildViewController:[self childNavigationControllerWithTitle:titles[i] imageName:images[i] index:i]];
    }
    
    self.tabBar.tintColor = QDSColorDefaultBlack;
    if (@available(iOS 10.0, *)) {
        self.tabBar.unselectedItemTintColor = QDSColorDefaultGray;//修改未选中
    } else {
        // Fallback on earlier versions
    }
}



- (UINavigationController *)childNavigationControllerWithTitle:(NSString *)title imageName:(NSString *)name index:(NSInteger)index{
    
    // navigationController's rootViewController
    UIViewController *viewController = [self classForNavigationRootViewControllerAtIndex:index].new;
    viewController.hidesBottomBarWhenPushed = false;
    
    // tabController's childViewController
    HBDNavigationController *navigationController = [[HBDNavigationController alloc]initWithNavigationBarClass:COHBDNavigationBar.class toolbarClass:nil];

    // 设置tab
    navigationController.viewControllers = @[viewController];
    navigationController.tabBarItem.title = title;
    navigationController.tabBarItem.image = name.ritl_image.ritl_renderOriginImage;
    navigationController.tabBarItem.selectedImage = [name stringByAppendingString:@"_selected"].ritl_image.ritl_renderOriginImage;
    
    return navigationController;
}



/// UITabController索引位置所在的控制器的类
- (Class)classForNavigationRootViewControllerAtIndex:(NSInteger)index {
    
    NSArray <NSString *> *navigationRootViewControllers = @[@"_TtC13QDMetroClient29QDMainPageTableViewController",
                                                            @"QDMetroCodeTableViewController",
                                                            @"_TtC13QDMetroClient31QDUserCenterTableViewController"];
    
    return objc_getClass(navigationRootViewControllers[index].UTF8String);//使用runtime比反射 速度快
}



- (void)setSelectedViewController:(__kindof UIViewController *)selectedViewController
{
    //获得当前selectedController
    NSInteger index = [self.viewControllers indexOfObject:selectedViewController];
    
    [super setSelectedViewController:selectedViewController];
    self.tabBarView.isSelected = (index == 1);
}


- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    [super setSelectedIndex:selectedIndex];
    self.tabBarView.isSelected = (selectedIndex == 1);
}


- (QDMainTabBar *)tabBarView
{
    if (!_tabBarView) {
        
        _tabBarView = [QDMainTabBar new];
        _tabBarView.delegate = self;
        _tabBarView.translucent = false;
        //        _tabBarView.backgroundColor = [NWThemeColor colorWithAlphaComponent:0.4];
    }
    
    return _tabBarView;
}



@end
